<template>
  <div class="constant-type-manage">
    <el-form :model="form" label-width="auto" style="max-width: 100%">
      <el-form-item>
        <el-button type="primary" @click="toAdd">添加</el-button>
      </el-form-item>
    </el-form>

    <div class="table-container">
      <el-table :data="tableData" style="width: 100%" stripe>
        <el-table-column prop="constanttypecode" label="常数类别编码" width="180" />
        <el-table-column prop="constanttypename" label="常数类别名称" width="180" />
        <el-table-column prop="delmark" label="删除标记" width="100" />

        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="toEdit(scope.row)">编辑</el-button>
            <el-button link type="danger" size="small" @click="toDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 添加/编辑表单对话框 -->
    <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑常数类别' : '添加常数类别'" width="500px">
      <el-form :model="formData" label-width="120px">
        <el-form-item label="常数类别编码">
          <el-input v-model="formData.constanttypecode" placeholder="请输入类别编码" />
        </el-form-item>
        <el-form-item label="常数类别名称">
          <el-input v-model="formData.constanttypename" placeholder="请输入类别名称" />
        </el-form-item>
        <el-form-item label="删除标记">
          <el-radio-group v-model="formData.delmark">
            <el-radio :label="0">未删除</el-radio>
            <el-radio :label="1">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { ElNotification, ElMessageBox } from 'element-plus'
import service from '@/utils/request'

// 表单数据
const form = ref({})
const formData = ref({
  constanttypecode: '',
  constanttypename: '',
  delmark: 0
})

// 对话框状态
const dialogVisible = ref(false)
const isEdit = ref(false)
const currentId = ref(null)

// 表格数据
const tableData = ref([])

// 获取数据列表
const listData = () => {
  service.get('/constanttype/list').then(res => {
    if (res.data) {
      tableData.value = res.data
    } else {
      ElNotification({
        title: '提示',
        message: '获取数据失败',
        type: 'error'
      })
    }
  }).catch(err => {
    console.error('获取数据出错', err)
    ElNotification({
      title: '错误',
      message: '服务器异常，请稍后再试',
      type: 'error'
    })
  })
}

// 添加数据
const toAdd = () => {
  isEdit.value = false
  currentId.value = null
  formData.value = {
    constanttypecode: '',
    constanttypename: '',
    delmark: 0
  }
  dialogVisible.value = true
}

// 编辑数据
const toEdit = (row) => {
  isEdit.value = true
  currentId.value = row.id
  formData.value = {
    ...row
  }
  dialogVisible.value = true
}

// 删除数据
const toDelete = (id) => {
  ElMessageBox.confirm(
      '确认删除该常数类别？此操作不可恢复。',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    service.delete(`/constanttype/delete/${id}`).then(res => {
      if (res.data) {
        ElNotification({
          title: '成功',
          message: '删除成功',
          type: 'success'
        })
        listData()
      } else {
        ElNotification({
          title: '失败',
          message: '删除失败',
          type: 'error'
        })
      }
    }).catch(err => {
      console.error('删除出错', err)
      ElNotification({
        title: '错误',
        message: '服务器异常，请稍后再试',
        type: 'error'
      })
    })
  }).catch(() => {
    // 取消删除
  })
}

// 提交表单
const submitForm = () => {
  // 简单校验
  if (!formData.value.constanttypecode) {
    ElNotification({
      title: '提示',
      message: '请输入类别编码',
      type: 'warning'
    })
    return
  }
  if (!formData.value.constanttypename) {
    ElNotification({
      title: '提示',
      message: '请输入类别名称',
      type: 'warning'
    })
    return
  }

  if (isEdit.value) {
    // 编辑操作
    service.put('/constanttype/update', formData.value).then(res => {
      handleSubmitResult(res, '更新')
    })
  } else {
    // 添加操作
    service.post('/constanttype/add', formData.value).then(res => {
      handleSubmitResult(res, '添加')
    })
  }
}

// 处理提交结果
const handleSubmitResult = (res, action) => {
  if (res.data) {
    ElNotification({
      title: '成功',
      message: `${action}成功`,
      type: 'success'
    })
    dialogVisible.value = false
    listData()
  } else {
    ElNotification({
      title: '失败',
      message: `${action}失败，请检查输入`,
      type: 'error'
    })
  }
}

onMounted(() => {
  listData()
})
</script>

<style scoped>
.constant-type-manage {
  padding: 20px;
}
.table-container {
  margin-top: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  overflow: hidden;
}
</style>